package com.example.smartwaterapi.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.smartwaterapi.entity.SystemConfig;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

/**
 * 系统配置Mapper接口
 */
@Mapper
public interface SystemConfigMapper extends BaseMapper<SystemConfig> {
    
    /**
     * 根据配置键获取配置值
     */
    @Select("SELECT config_value FROM system_config WHERE config_key = #{configKey}")
    String getConfigValue(@Param("configKey") String configKey);
    
    /**
     * 获取指定分组的所有配置
     */
    @Select("SELECT config_key, config_value FROM system_config WHERE group_name = #{groupName}")
    List<Map<String, String>> getConfigsByGroup(@Param("groupName") String groupName);
    
    /**
     * 更新配置值
     */
    @Update("UPDATE system_config SET config_value = #{configValue}, updated_at = NOW() WHERE config_key = #{configKey}")
    int updateConfigValue(@Param("configKey") String configKey, @Param("configValue") String configValue);
    
    /**
     * 获取所有配置的分组
     */
    @Select("SELECT DISTINCT group_name FROM system_config ORDER BY group_name")
    List<String> getAllConfigGroups();
    
    /**
     * 批量获取配置值
     */
    @Select("<script>" +
            "SELECT config_key, config_value FROM system_config " +
            "WHERE config_key IN " +
            "<foreach collection='keys' item='key' open='(' separator=',' close=')'>" +
            "#{key}" +
            "</foreach>" +
            "</script>")
    List<Map<String, String>> batchGetConfigValues(@Param("keys") List<String> configKeys);
} 